<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ExpressCompany;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ExpressCompanyRepository;
use App\Http\Requests\Admin\ExpressCompany\ShowRequest;
use App\Http\Requests\Admin\ExpressCompany\QueryResource;
use App\Http\Requests\Admin\ExpressCompany\ExpressCompanyCreate;
use App\Http\Requests\Admin\ExpressCompany\ExpressCompanyUpdate;
use App\Http\Requests\Admin\ExpressCompany\ExpressCompanyDestroy;

class ExpressCompanyController extends Controller
{
    protected $expressCompanyRepository;

    /**
     * ExpressCompanyController constructor.
     * @param ExpressCompanyRepository $expressCompanyRepository
     */
    public function __construct(
        ExpressCompanyRepository $expressCompanyRepository
    )
    {
        $this->expressCompanyRepository = $expressCompanyRepository;
    }

    /**
     * 获取快递公司列表资源列表
     *
     * Date: 2018/05/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $expressCompany_name = $request->get('expressCompany_name', false);
        $code_name = $request->get('code_name', false);
        $phone = $request->get('phone', false);
        $limit = $request->get('paginate', false);
        $expressCompany = $this->expressCompanyRepository->getExpressCompanys($expressCompany_name, $limit, $code_name, $phone);
        return success($expressCompany);
    }

    /**
     * 获取快递公司详情
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ShowRequest $request
     * @param ExpressCompany $express
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, ExpressCompany $express)
    {
        return success($express);
    }


    /**
     * 创建快递公司
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ExpressCompanyCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(ExpressCompanyCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $expressCompanys = $this->expressCompanyRepository->create($attributes);
        return stored($expressCompanys);
    }

    /**
     * 更新快递公司资源
     *
     * Date: 2018/05/11
     * @author Tenny
     * @param ExpressCompanyUpdate $request
     * @param expressCompany $express
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(ExpressCompanyUpdate $request, ExpressCompany $express)
    {
        $expressCompanyAttr = $request->only([
            'express_name',
            'express_no',
            'code_name',
            'status',
            'phone',
            'express_logo',
            'region_mark',
        ]);
        $express->update($expressCompanyAttr);
        return updated($express);
    }

    /**
     * 删除 快递公司 资源
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ExpressCompanyDestroy $request
     * @param ExpressCompany $express
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(ExpressCompanyDestroy $request, ExpressCompany $express)
    {
        //判断是否有网点， 有就不允许删除  没有才允许删除
        $express_id = $express->id;
        $network = Network::query()
            ->where('express_id', '=', $express_id)
            ->first();
        if (!$network) {
            $express->delete();
        } else {
            return failed('不允许删除该物流公司信息', 403);
        }
        return deleted();
    }
}
